<?php
class Admin extends CActiveRecord {
	public static function model($className = __CLASS__) {
		parent::model ( $className );
	}
	public function tableName() {
		return '{{admin}}';
	}
	public function rules() {
		
		return array (
			
			array (
				
				'passport,pwd,group', 
				'required', 
				'message' => '不能为空' 
			), 
			array (
				
				'passport', 
				'unique', 
				'message' => '已存在' 
			), 
			array (
				
				'name,phone,email', 
				'safe' 
			) 
		);
	}
	public function relations() {
		
		return array (
			
			'groupName' => array (
				
				self::BELONGS_TO, 
				'Groups', 
				'group' 
			) 
		);
	}
	public function attributeLabels() {
		return array (
			
			"id" => "id", 
			"name" => "name", 
			"pwd" => "pwd", 
			"group" => "group", 
			"phone" => "phone", 
			"passport" => "passport", 
			"email" => "email", 
			"state" => "state", 
			"createtime" => "createtime" 
		);
	}
	public function getAdmin() {
		$ret = $this->findAll ( array (
			
			"condition" => "state=1 and passport != 'admin'" 
		) );
		$rows = CommonFunc::YiiobjectToArray ( $ret, "all" );
		return $rows;
	}
	public function getAdminListByPage($criteria) {
		$ret = $this->with ( 'groupName' )->findAll ( $criteria);
		$rows = CommonFunc::YiiobjectToArray ( $ret, "all" ,"groupName");
		if ($rows) {
			return $rows;
		}
		return FALSE;
	}
	public function getCount($criteria){
		return $this->with ( 'groupName' )->count($criteria);
	}
	protected function beforeSave() {
		if (parent::beforeSave ()) {
			if ($this->isNewRecord) {
				$this->pwd = md5 ( $this->pwd );
				$this->createtime = time ();
			}
			return true;
		} else
			return false;
	}

}
?>